import { _decorator, Component, Label, Node, tween, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('timeBg')
export class timeBg extends Component {

    private time: number = 10


    start() {
        this.starTimeCheck()
    }


    starTimeCheck() {
        // 以秒为单位的时间间隔
        let interval = 1;
        this.schedule(this.callback, interval);
    }

    callback() {
        if (this.time == 0) {
            this.node.getChildByName('time').getComponent(Label).string = '00:00'
            this.unschedule(this.callback);
            //时间到。失败
            console.log('游戏失败')
            return
        }

        if (this.time = 30) {
            tween(this.node)
                .to(0.2, { scale: new Vec3(1.2, 1.2, 1) }, { easing: easing.sineInOut }) // 放大到1.2倍
                .to(0.2, { scale: new Vec3(1, 1, 1) }, { easing: easing.sineInOut }) // 缩小到1倍
                .repeatForever() // 无限循环
                .start(); // 开始动画
            // union().
            // repeat(3).
            // start()

            //播放一个音效
        }
        this.time--;
        let timeCur = this.formatTime(this.time)
        this.node.getChildByName('time').getComponent(Label).string = timeCur



    }

    formatTime(seconds: number): string {
        const minutes = Math.floor(seconds / 60);  // 计算分钟数
        const remainingSeconds = seconds % 60;    // 计算剩余的秒数
        if (remainingSeconds < 10) {
            return `0${minutes}:0${remainingSeconds}`;
        } else {
            return `0${minutes}:${remainingSeconds}`;
        }

    }
}


